<?php defined('BASEPATH') || exit('No direct script access allowed');

class Viewlog_model extends BF_Model
{
    // 新增属性，每日分表记录数据
    protected $db_con   = 'log';
    public $is_chip     = true;
    public $chip_date   = '';
    // 新增属性结束

    protected $table_name	= 'view_log';
	protected $key			= 'id';
	protected $date_format	= 'datetime';

	protected $log_user 	= false;
    protected $set_created	= true;
	protected $set_modified = false;//true;
	protected $soft_deletes	= false;

	protected $created_field     = 'created_on';
	protected $modified_field    = 'modified_on';
    protected $deleted_field     = 'deleted';

	// Customize the operations of the model without recreating the insert,
    // update, etc. methods by adding the method names to act as callbacks here.
	protected $before_insert 	= array('set_table_name_create');
	protected $after_insert 	= array();
	protected $before_update 	= array('get_table_name');
	protected $after_update 	= array();
	protected $before_find 	    = array('get_table_name');
	protected $after_find 		= array();
	protected $before_delete 	= array('get_table_name');
	protected $after_delete 	= array();

	// For performance reasons, you may require your model to NOT return the id
	// of the last inserted row as it is a bit of a slow method. This is
    // primarily helpful when running big loops over data.
	protected $return_insert_id = true;

	// The default type for returned row data.
	protected $return_type = 'object';

	// Items that are always removed from data prior to inserts or updates.
	protected $protected_attributes = array();

	// You may need to move certain rules (like required) into the
	// $insert_validation_rules array and out of the standard validation array.
	// That way it is only required during inserts, not updates which may only
	// be updating a portion of the data.
	protected $validation_rules 		= array(
	
	);
	protected $insert_validation_rules  = array();
	protected $skip_validation 			= false;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function prep_data($post_data) {
        $data = parent::prep_data($post_data);

        return $data;
    }

    /**
     * 新增数据时获取表名称
     * @param object $row
     */
    public function set_table_name_create($row) {
        $arr = explode('_', $this->table_name);
        if ($this->is_chip && !is_numeric(end($arr))) {
            // 分表
            $tableChip = $this->table_name.'_'.date("Ymd");
            if (!$this->db->table_exists($tableChip)) {
                // $this->load->dbforge();
                // $this->dbforge->create_table($this->table_name, true, array('like'=>$baseTableName));
                $this->db->simple_query("CREATE TABLE IF NOT EXISTS `{$tableChip}` LIKE `{$this->table_name}`;");
            }
            $this->table_name = $tableChip;
        }
        return $row;
    }

    /**
     * 根据日期获取已存在表名称
     * @param  object $row
     * @return object
     */
    public function get_table_name($row) {
        $arr = explode('_', $this->table_name);
        if ($this->is_chip && !is_numeric(end($arr)) && !empty($this->chip_date)) {
            // 分表
            $tableChip = $this->table_name.'_'.$this->chip_date;
            $this->table_name = $tableChip;
        }
        return $row;
    }

    public function insert($data = null) {
        return parent::insert($data);
    }
}